package models;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Query;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.log4j.chainsaw.Main;


import play.db.jpa.Model;
@Entity
@Table (name = "saleorder")
public class SaleOrder extends Model{
	@Column(name = "saleorderno")
	private String saleorderno;
	@Column(name = "customerno")
	private String customerno;
	@Column(name = "total")
	private double total;
	@Column(name = "createdatetime")
	private long createdatetime;
	@Transient
	private static int i = 0;
	
	public SaleOrder () {
		this.createdatetime=new Date().getTime();
		//this.saleorderno=generateOrderno();
	}
	
	public static String generateOrderno(){
		
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
		String dateString = df.format(new Date());		
	
		//设置流水号		
		//查找最新一条记录
		SaleOrder saleOrder = SaleOrder.find("order by createdatetime desc").first();
		if(saleOrder != null){
		long newtime = saleOrder.createdatetime;
		long nowtime = new Date().getTime();		
		try{             //如果同一天流水号才增加
			if((nowtime-newtime)/(24*3600*1000) == 0) 
				i++;
			else         //否则流水号要从0开始
				i = 0;
		}catch (Exception exception) {
            exception.printStackTrace();
			}	
		}
			String istr = String.valueOf(i);
			if(i < 10) 
				istr = "000" + istr;
			else if (i<100)  
				istr = "00" + istr;
			else if (i<1000)  
				istr = "0" + istr;		
		return "WYS"+dateString+istr;		
	}
	
	public String getSaleorderno() {
		return saleorderno;
	}
	public void setSaleorderno(String saleorderno) {
		this.saleorderno = saleorderno;
	}
	public String getCustomerno() {
		return customerno;
	}
	public void setCustomerno(String customerno) {
		this.customerno = customerno;
	}
	public Double getTotal() {
		return total;
	}
	public void setTotal(Double total) {
		this.total = total;
	}
	public long getCreatedatetime() {
		return createdatetime;
	}
	public void setCreatedatetime(long createdatetime) {
		this.createdatetime = createdatetime;
	}

}
